import threading

from playwright.sync_api import sync_playwright

# param = {"loginId": "7196", "password": "c88fdedd61a40a44ed13c4eb6d958454", "isMobile": "0"}
# url = 'https://uatmall.yaoud.cn:9030/api/businessService/pc/front/user/loginGetBuyers'
# response = requests.post(url, json=param)
#
# print(f'响应头:{response.headers}')
# print(f'状态码:{response.status_code}')
# print(f'响应体-文本格式:{response.text}')
# print(f'响应体-json格式:{response.json()}')
# print(f'raw:{response.raw}')
# print(f'cookie:{response.cookies}')
# print(f'cookie字典格式:{response.cookies.get_dict()}')
# print(f'响应头-token：{response.headers.get("x-access-token-header")}')

url = "https://test-salus.huahill.com/api/cdp/member/pos/create"
headers = {
    "Client-Tom": "Y",
    "TENANT-ID": "3",
    "Authorization": "Bearer f01725d4-f576-4b0f-a374-d3409d5a8ffc"
}
data = {
    "name": "dm测注册接口",
    "sex": "1",
    "phone": None,
    "birthDate": "2024-04-01",
    "ownerEmp": "戴茂",
    "ownerEmpId": "29",
    "stoId": "8",
    "stoName": "西安泰生桥梓口二店",
    "cardStoId": "8",
    "cardStoName": "西安泰生桥梓口二店",
    "channelCode": "POS",
    "homeProvince": "430000000000",
    "homeCity": "430100000000",
    "homeSection": "430121000000",
    "tempAddress": "湖南省长沙市长沙县",
    "homeAddress": "",
    "idCardCode": "",
    "idCardType": "1",
    "remarks": "批量发券测试所需基础数据，脚本大批量创建会员",
    "labelList": [{
        "id": "1311",
        "tenantId": "3",
        "createTime": "2023-12-21 17:18:14",
        "updateTime": "2023-12-21 17:18:14",
        "createId": "37",
        "createName": "戴茂",
        "updateId": "37",
        "updateName": "戴茂",
        "deleted": "0",
        "revision": "0",
        "entFlag": "0",
        "objType": "client_management",
        "labelTypeId": "299",
        "labelColor": "#C62525",
        "labelDetails": "小会员",
        "organId": None,
        "systemId": "5",
        "entId": None
    }, {
        "id": "1312",
        "tenantId": "3",
        "createTime": "2023-12-21 17:18:24",
        "updateTime": "2023-12-21 17:18:24",
        "createId": "37",
        "createName": "戴茂",
        "updateId": "37",
        "updateName": "戴茂",
        "deleted": "0",
        "revision": "0",
        "entFlag": "0",
        "objType": "client_management",
        "labelTypeId": "299",
        "labelColor": "#1ACE9B",
        "labelDetails": "小小会员",
        "organId": None,
        "systemId": "5",
        "entId": None
    }, {
        "id": "1407",
        "tenantId": "3",
        "createTime": "2024-04-11 13:58:58",
        "updateTime": "2024-04-11 13:58:58",
        "createId": "1",
        "createName": "超级管理员",
        "updateId": "1",
        "updateName": "超级管理员",
        "deleted": "0",
        "revision": "0",
        "entFlag": "1",
        "objType": "client_management",
        "labelTypeId": "307",
        "labelColor": "#F7940B",
        "labelDetails": "很好看",
        "organId": "70",
        "systemId": "4",
        "entId": None
    }]
}


def create_member(phone, name, count):
    with sync_playwright() as pl:
        rq = pl.request.new_context()
        for i in range(count):
            data["name"] = f"批量发券d{name}{i:05}"
            data["phone"] = f"1878{phone}0{i:05}"
            data["sex"] = f"{i % 2 + 1}"
            response = rq.post(url=url, headers=headers, data=data)
            # print(response.json())


def run_threads(thread_count, member_count):
    """
    :param thread_count : 并发线程数
    :param member_count : 每个线程创建的会员数
    """
    # 创建并启动线程
    threads = []
    for i in range(thread_count):
        t = threading.Thread(target=create_member, args=(i, i, member_count))
        t.start()
        threads.append(t)
    # 等待所有线程完成
    for t in threads:
        t.join()
        print(t.name, "线程已完成")
    print("所有线程已完成")


if __name__ == '__main__':
    run_threads(10, 10000)
